#ifndef _DL40_H_
#define _DL40_H_

extern uint32_t SystemFrequency;

#define MANUFACTURER_GHI 0x10
#define MODULE_TYPE		 0x01 
#define MODULE_VERSION	1
/////////////////////////
#define REGS_SIZE (DL_REGS_SIZE + 1)		// CHANGE MEH
#define I2C_SCL_PU_PORT 0
#define I2C_SCL_PU_PIN 3

#define I2C_SDA_PU_PORT	1
#define I2C_SDA_PU_PIN	9

// Upstream 
#define US_PORT 3
#define US_PIN 4

#define NanoSecond 				(1000000000)

#define PINCOUNT					(42)
#define ADC_CHANNEL_COUNT	(8)

#define REG_FIRSTPINCONF 	(REGS_SIZE)
#define REG_LASTPINCONF		(REG_FIRSTPINCONF + PINCOUNT - 1)

#define REG_FIRSTCOMMAND	(REG_LASTPINCONF + 1)
#define REG_LASTCOMMAND		(REG_FIRSTCOMMAND + PINCOUNT - 1)

#define REG_FIRSTPINARG 	(REG_LASTCOMMAND + 1)
#define REG_LASTPINARG		(REG_FIRSTPINARG + PINCOUNT - 1)

#define REG_FIRSTPINVALUE	(REG_LASTPINARG + 1)
#define REG_LASTPINVALUE	(REG_FIRSTPINVALUE + PINCOUNT - 1)

#define REG_FIRSTPINCOUNT	(REG_LASTPINVALUE + 1)
#define REG_LASTPINCOUNT	(REG_FIRSTPINCOUNT + PINCOUNT - 1)

#define REG_FIRST_ADC_VALUE (REG_LASTPINCOUNT + 1)
#define REG_LAST_ADC_VALUE = (REG_FIRST_ADC_VALUE + ADC_CHANNEL_COUNT - 1)

// Output pin
#define CONFIGPIN_OUT				(0)

// Input pin
#define CONFIGPIN_IN				(1)

// Interrupt pin
#define CONFIGPIN_INT				(2)

// Counter pin
#define CONFIGPIN_COUNTER		(3)

// Capture pin
#define CONFIGPIN_CAPTURE   (4)

// ADC Input pin
#define CONFIGPIN_ADC				(5)

// PWM pin
#define CONFIGPIN_PWM				(6)

// Interrupt PIN Settings
#define InterruptEdgeLow 					(1)
#define InterruptEdgeHigh 				(2)
#define InterruptEdgeBoth 				(3)
#define InterruptEdgeLevelHigh		(4)
#define InterruptEdgeLevelLow 		(5)

#define CONFIGPIN_TYPEMASK				(0x0F)
#define CONFIGPIN_MODEMASK				(0xF0)
#define CONFIGPIN_PULLUP					(0x00 << 4)
#define CONFIGPIN_PULLDOWN				(0x01 << 4)
#define CONFIGPIN_PULLNONE				(0x02 << 4)
#define CONFIGPIN_REPEATER				(0x03 << 4)

#define TIMER_B16_0_PIN						(2)
#define TIMER_B16_1_PIN						(20)
#define TIMER_B32_0_PIN						(17)
#define TIMER_B32_1_PIN						(12)

#define PWM_CT16B0_MAT0_PIN				(0 * 12 + 8)
#define PWM_CT16B0_MAT1_PIN				(0 * 12 + 9)
#define PWM_CT16B0_MAT2_PIN				(0 * 12 + 10)
#define PWM_CT32B1_MAT0_PIN				(1 * 12 + 1)
#define PWM_CT32B1_MAT1_PIN				(1 * 12 + 2)
#define PWM_CT32B1_MAT2_PIN				(1 * 12 + 3)
#define PWM_CT16B1_MAT0_PIN				(1 * 12 + 9)
#define PWM_CT16B1_MAT1_PIN				(1 * 12 + 10)

// Common command
#define CMD_COMMON_CONFIG					(255)

// Counter PIN commands
#define CMD_COUNTER_RESET					(1)

// Output PIN commands
#define CMD_OUTPUTPIN_SET					(1)

// Counter Flags
#define FLAG_RESETONREAD					(1)

// PWM commands
#define CMD_PWMOUTPUTPIN_SETPULSE	(1)
#define CMD_PWMOUTPUTPIN_RESET		(2)

struct Reg_t
{
	struct DL_Regs dl;
	BYTE InterruptActive;	
} __attribute__((__packed__));

extern struct Reg_t Reg;

typedef struct tagTPinConfig
{
	volatile BYTE			Config;
	volatile BYTE			Command;
	volatile BYTE			Arguments[8];
	volatile BYTE			InterruptFlag;
	volatile WORD			Flags;
} TPinConfig;

typedef struct tagPinCount
{
	volatile UINT64		Count;
	volatile UINT64		LastCount;
} TPinCount;

typedef struct 
{
	BYTE reg;
	BYTE offset;
	BYTE value;
	unsigned int ticks;
} BufferIn_t;

extern TPinConfig g_pins[];
extern TPinCount  g_pinCounters[];

#endif //_DL40_H_
